﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/Permission.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

var ID;
$(document).ready(function () {
    $("#btnSave").bind("click", Save);
    $("#btnCheckAll").click(function(){
        $("[name='permission']").attr("checked",'true');
  
    });
    $("#btnUnCheckAll").click(function(){
        $("[name='permission']").removeAttr("checked");
    });
    ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        BindData();
    }
});

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("PermissionList.aspx");
        }
    });
}
function GetData() {
    var formIDs=new Array();
    var arrChk=$("input[name='permission'][checked]");
                $(arrChk).each(function(){
                    formIDs.push($(this).val());
                }); 
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "FormIDs":encodeURIComponent(formIDs.join("|")),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val())
    }

    return ret;
}

function IsValidate() {
    return true;
}

function BindData() {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).Permissions[0];
                $("#UserName").html(decodeURIComponent(obj.UserName));
                $("#DepName").html(decodeURIComponent(obj.DepName));
                var formIDs=decodeURIComponent(obj.FormIDs).split(',');
                $("input[name='permission']").each(function(){
                    if(formIDs.contains($(this).val()))
                    {
                        $(this).attr("checked",'true');
                    }
                }); 
            }
        });
}